const http = uni.$u.http
import config from '@/common/config.js'

const logout = () => http.get('/paas-auth/oauth/logout');


const getToken = (data) => {
	const url = `/paas-resource/sms/endpoint/validate-message`
	return http.post(url, data, {
		header: {
			'Content-Type': 'application/x-www-form-urlencoded',
		}
	})
}

const refreshTokenApi = (refreshToken) => {
	const url = `/paas-auth/wx/ma/oauth/token`
	return http.post(url, {}, {
		params: {
			refresh_token: refreshToken,
			grant_type: "wx_ma_refresh_token",
		}
	})
}

const getDriver = (params) => {
	const url = `/paas-basic/app/project-work-record/search-drivers`
	return http.get(url, {
		params
	})
}

const getWxOpenId = (params) => {
	const url = `/paas-auth/oauth/jscode2session`
	return http.get(url, {
		params
	})
}

const loginByWechat = ({
	openId,
	tenantId,
	nickName,
	avatar,
	phone,
	countryCode,
	promotionId
}) => {
	const params = {
		grant_type: 'wechat',
		scope: "all",
		openId,
		tenantId: '000000',
		nickName,
		avatar,
		phone,
		countryCode,
		promotionId
	}
	const url = `/paas-auth/oauth/token`
	return http.post(url, {}, {
		params
	})
}

const registeByWechat = ({
	openId,
	account,
	password,
	tenantId,
	phone
}) => {
	const params = {
		openId,
		account,
		password,
		tenantId,
		phone
	}
	const url = `/paas-auth/oauth/wx-register`
	return http.post(url, {}, {
		params
	})
}


const bindWechat = ({
	openId,
	account,
	password,
	tenantId
}) => {
	const params = {
		openId,
		account,
		password,
		tenantId
	}
	const url = `/paas-auth/oauth/wx-bind`
	return http.post(url, {}, {
		params
	})
}

const refreshToken = (refresh_token, tenantId) => {
	const params = {
		tenantId,
		refresh_token,
		grant_type: "refresh_token",
		scope: "all",
	}
	const url = `/paas-auth/oauth/token`
	return http.post(url, {}, {
		params,
		header: {
			'Tenant-Id': tenantId,

		}
	})
}


const checkBindDms = () => {
	const url = `/paas-listen/maintain/check-dms-user`
	return http.get(url)
}

const bindDms = (data) => {
	const url = `/paas-user/bind-dms-user`
	return http.post(url, data, {})
}



const getUserInfo = () => {
	const url = '/paas-user/info'
	return http.get(url)
}

const updateUserInfo = (data) => {
	const url = '/paas-user/update-info'
	return http.post(url, data)
}


const updateUserPhone = (data) => {
	const url = '/paas-user/update-phone'
	return http.post(url, data)
}


// 手机验证码
const getValidate = (params) => {
	const url = '/paas-resource/sms/endpoint/send-validate'
	return http.post(url, {}, {
		params
	})
}
const validateMessage = (params) => {
	const url = '/paas-resource/sms/endpoint/validate-message'
	return http.post(url, {}, {
		params
	})
}



const getWxPhoneByCode = (params) => {
	const url = '/paas-wechat/wechat/get/phone'
	return http.get(url, {
		params
	})
}

const getRegistDmsProtocal = (protocalCode) => {
	const url = `${config.dmsBaseUrl.basedata}/file/protocol/${protocalCode}`
	return http.get(url)
}

const getRegistDmsCaptcha = (phone) => {
	const url = `${config.dmsBaseUrl.service}/shortMessage/sendMessage/${phone}/interAspect?0=${phone}`
	return http.get(url)
}

const registDms = (params) => {
	const url = `${config.dmsBaseUrl.basedata}/basedata/authorization/saveAppUserInfo/interAspect`
	return http.get(url, {
		params
	})
}

const getDmsInfo = () => {
	const url = `/paas-listen/maintain/dms-user-info`
	return http.get(url, {})
}

const unbindDms = () => {
	const url = `/paas-listen/maintain/unbind-dms-user`
	return http.get(url, {})
}




// 新登录接口
const loginByCode = ({
	code,
	promotionId,
}) => {
	const params = {
		code,
		grant_type: 'wx_ma',
		promotionId
	}
	const url = `/paas-auth/wx/ma/oauth/token`
	return http.post(url, {}, {
		params
	})
}

const updateWxinfo = ({
	avatar,
	countryCode,
	nickName,
	phone
}) => {
	const data = {
		avatar,
		countryCode,
		nickName,
		phone
	}
	const url = `/paas-listen/wechat/user/update`
	return http.post(url, data)
}


const checkVoucher = () => {
	const url = '/paas-repository/user/voucher/notify/status'
	return http.get(url)
}




export default {
	logout,
	getToken,
	refreshTokenApi,
	getDriver,
	getWxOpenId,
	loginByWechat,
	registeByWechat,
	bindWechat,
	refreshToken,
	checkBindDms,
	bindDms,
	getUserInfo,
	updateUserInfo,
	updateUserPhone,
	getValidate,
	validateMessage,
	getWxPhoneByCode,
	getRegistDmsProtocal,
	getRegistDmsCaptcha,
	registDms,
	getDmsInfo,
	unbindDms,
	// 新登录接口
	loginByCode,
	updateWxinfo,
	
	// 检测购机券
	checkVoucher,
}
